package com.yanfan.zutai.controller;

import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.zutai.aop.SysInitData;
import com.yanfan.zutai.aop.SysInitUpdateData;
import com.yanfan.zutai.aop.SysQueryData;
import com.yanfan.zutai.domain.BDeviceWarn;
import com.yanfan.zutai.domain.BWeiXinUser;
import com.yanfan.zutai.service.IBWeiXinService;
import com.yanfan.zutai.service.IBWeiXinUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 公众号用户Controller
 *
 * @author ruoyi
 * @date 2023-04-11
 */
@RestController
@RequestMapping("/zutai/bWeiXinUser")
public class BWeiXinUserController extends BaseController {
    @Autowired
    private IBWeiXinUserService bWeiXinUserService;
    @Autowired
    private IBWeiXinService bWeiXinService;

    /**
     * 查询公众号用户列表
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bWeiXinUser:list')")
    @GetMapping("/list")
    @SysQueryData
    public TableDataInfo list(BWeiXinUser bWeiXinUser) {
        startPage();
        List<BWeiXinUser> list = bWeiXinUserService.selectBWeiXinUserList(bWeiXinUser);
        return getDataTable(list);
    }

    /**
     * 导出公众号用户列表
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bWeiXinUser:export')")
    @Log(title = "公众号用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @SysQueryData
    public void export(HttpServletResponse response, BWeiXinUser bWeiXinUser) {
        List<BWeiXinUser> list = bWeiXinUserService.selectBWeiXinUserList(bWeiXinUser);
        ExcelUtil<BWeiXinUser> util = new ExcelUtil<BWeiXinUser>(BWeiXinUser.class);
        util.exportExcel(response, list, "公众号用户数据");
    }

    /**
     * 获取公众号用户详细信息
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bWeiXinUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bWeiXinUserService.selectBWeiXinUserById(id));
    }

    /**
     * 新增公众号用户
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bWeiXinUser:add')")
    @Log(title = "公众号用户", businessType = BusinessType.INSERT)
    @PostMapping
    @SysInitData
    public AjaxResult add(@RequestBody BWeiXinUser bWeiXinUser) {
        return toAjax(bWeiXinUserService.insertBWeiXinUser(bWeiXinUser));
    }

    /**
     * 修改公众号用户
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bWeiXinUser:edit')")
    @Log(title = "公众号用户", businessType = BusinessType.UPDATE)
    @PutMapping
    @SysInitUpdateData
    public AjaxResult edit(@RequestBody BWeiXinUser bWeiXinUser) {
        // bWeiXinUserService.insertOpenIdAndUser();
        BDeviceWarn bDeviceWarn = new BDeviceWarn();
        bDeviceWarn.setUpdateBy(bWeiXinUser.getUserName().split("-")[0]);
        bDeviceWarn.setWarnTime(new Date());
        bDeviceWarn.setWarnMessage(bWeiXinUser.getMessage());
        bDeviceWarn.setWarnType(bWeiXinUser.getMessage());
        bDeviceWarn.setDeviceName("公众号测试设备");
        bDeviceWarn.setAddress("公众号测试地址");
        bDeviceWarn.setDeviceMac("123456789");
        bWeiXinService.sendMsg(bDeviceWarn);
        return AjaxResult.success();
    }

    /**
     * 删除公众号用户
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bWeiXinUser:remove')")
    @Log(title = "公众号用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bWeiXinUserService.deleteBWeiXinUserByIds(ids));
    }
}
